package com.hejing.cloud.controller;

import com.hejing.cloud.entities.Pay;
import com.hejing.cloud.entities.PayDTO;
import com.hejing.cloud.resp.ResultData;
import com.hejing.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: PayController
 * Package: com.hejing.cloud.controller
 * Description:
 *
 * @Author: HeJing
 * @Create: 2024-04-25 22:42
 * @Version: 1.0
 */
@RestController
@Slf4j
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法，json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {

        System.out.println(pay.toString());
        int id = payService.add(pay);
        return ResultData.success("成功插入记录，返回值: " + id);
    }

    @DeleteMapping("/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @PutMapping("pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int id = payService.update(pay);

        return ResultData.success("成功修改记录，返回值：" + id);
    }

    @GetMapping("/pay/get/{id}")
    @Operation(summary = "按照ID查询流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new RuntimeException("id 不能为负数");
        }

        // int a = 10 / 0;  // 测试全局异常处理器
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @GetMapping("pay/get")
    @Operation(summary = "查询所有流水", description = "查询所有支付流水方法")
    public ResultData<List<Pay>> getAll() {

        List<Pay> all = payService.getAll();
        return ResultData.success(all);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(@Value("${hejing.info}") String hejingInfo) {

        return "hejingInfo: " + hejingInfo + "\t" + "port: " + port;
    }

}



































